package com.ssyu.examination.conmmon.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.entity.MenuEntity;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;

@WebServlet("/MainServlet")
public class MainServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public MainServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response)

	throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("listMenu".equals(command)) {
			listMenu(request, response);
		}
		// else if ("listTalent".equals(command))
		// {
		/*
		 * listTalent(request, response); } else if
		 * ("addTalent".equals(command)) { addTalent(request, response); } else
		 * if ("getTalent".equals(command)) { getTalent(request, response); }
		 * else if ("updateTalent".equals(command)) { updateTalent(request,
		 * response); } else if ("deleteTalent".equals(command)) {
		 * deleteTalent(request, response); }
		 */
	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String password = request.getParameter("password");

		if ("zs".equals(code) && "123456".equals(password)) {
			response.getWriter().print(1);
		}
	}

	private void listMenu(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		// SELECT * FROM sys_menu WHERE FIND_IN_SET(id,SELECT menuIds FROM role=?) pid=?
		List<MenuEntity> menuList = DBUtil.executeQuery(MenuEntity.class,
				"SELECT * FROM sys_menu WHERE pid=? ORDER BY sort", id);

		response.setContentType("text/html; charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.println(FastjsonUtil.toJson(menuList));

		writer.flush();
		writer.close();
	}

	/*
	 * private void listTalent(HttpServletRequest r st, H letResponse response)
	 * throws IOException { int page =
	 * Integer.parseInt(request.getParameter("page")); int rows =
	 * Integer.parseInt(request.getParameter("rows")); String sort =
	 * request.getParameter("sort"); String order =
	 * request.getParameter("order");
	 * 
	 * List<TalentEntity> talentList = DBUtil.executeQuery(TalentEntity.class,
	 * "SELECT * FROM talent ORDER BY " + sort + " " + " LIM - 1) * rows + "," +
	 * rows); List<TalentEntity> talentList2 = DBUtil.executy( TalentEntity.c
	 * "SELEFROM talent");
	 * 
	 * response.setContentType("text/html; charset=UTF-8"); PrintWriter writer =
	 * response.getWriter(); writer.println("{\"total\":\"" + talentList2.size()
	 * + "\", \"r :" + FastjsonUtil.toJ alentList) "); writer.flush();
	 * writer.close(); }
	 * 
	 * private void addTalent(HttpServletRequest r st, HttpServletResponse re
	 * se) throws IOE tion { String code = request.getParameter("code"); String
	 * password = request.getParameter("password"); String name =
	 * request.getParameter("name"); String phone =
	 * request.getParameter("phone"); String email =
	 * request.getParameter("email"); int age =
	 * Integer.parseInt(request.getParameter("age")); int gender =
	 * Integer.parseInt(request.getParameter("gender"));
	 * 
	 * int row =il .execute "INSERT INTent VALUES(NULL ?,, ?, ?, NULL, N code,
	 * password, name, phone, emai gend r);
	 * 
	 * if (row == 1 respo se.setContentType("text/html; charset=UTF-8");
	 * PrintWriter writer = response.getWriter(); // 坑，为什么调用write(1)不行？
	 * writer.print(1); writer.flush(); writer.close(); } }
	 * 
	 * private void getTalent(HttpServletRequest r st, HttpServletResponse
	 * response) throws IO ption { int id = Integer.pars
	 * (request.getParameter("id"));
	 * 
	 * TalentEntity talent = DBUtil.
	 * 
	 * getUniqueResult(TalentEntity s, "SELECT * FROM talent WHERE id=?", id);
	 * 
	 * talent != null) {
	 * 
	 * response.setC tType("text/html; charset=UTF-8"); PrintWriter writer =
	 * response.getWriter(); writer.print(FastjsonUtil.toJson(talent));
	 * writer.flush(); writer.close(); }
	 * 
	 * }
	 * 
	 * private void updateTalent(HttpServletRequest r st, HttpServletResponse
	 * response) throws IOExcepti int id = Integer.parseInt(
	 * 
	 * request.g rameter("id")); String code = request.getParameter("code");
	 * String name = request.getParameter("name"); String phone =
	 * request.getParameter("phone"); String email =
	 * request.getParameter("email"); int age =
	 * Integer.parseInt(request.getParameter("age")); int gender =
	 * Integer.parseInt(request.getParameter("gender"));
	 * 
	 * int row =il .execute "UPDATE talent SET code=?,name=?,ph,email=?,age=?,?
	 * WHERE
	 * 
	 * code, nahone, email, ager, id);
	 * 
	 * if (row == response.setContentType("text/html; charset=UTF PrintWriter
	 * writer = response.getWriter(); writer.print(1); writer.flush();
	 * writer.close(); }
	 * 
	 * }
	 * 
	 * private void deleteTalent(HttpServletRequest r st, HttpServletResponse
	 * response) throws IOException { String ids equest.getParameter("ids"); int
	 * row =
	 * 
	 * DBUtil.executeU e("DELETE FROM talent WHERE id IN ( ds + ")");
	 * 
	 * if (row >= 1) { response.setContentType("text/html;  et=UTF-8");
	 * PrintWriter writer = response.getWriter(); iter.print(1); writer.flush();
	 * writer.close(); } }
	 */
}
